#ifndef __IMA_DB_INTERFACE_H__
#define __IMA_DB_INTERFACE_H__

#include "ImaConf.h"
#include "postgresql/libpq-fe.h"
#include <string>
#include <vector>

using namespace std;

typedef struct recipient_details {
	unsigned long id;
	char name[128];
	char address[1024];
	char emailId[256];
	char phone[32];
	char relationship[256];
} RECIPIENT_DETAILS;

typedef struct info_details {
	unsigned long id;
	char info[4096];
	short media;
	char doc[32];
	char doe[32];
	unsigned long ttl;
	std::vector<unsigned long> recipients;
} INFO_DETAILS;

class ImaDbInterface {
	public:
		static ImaDbInterface* getInstance(ImaConf& conf);
		~ImaDbInterface();
		int isRegisteredUser(const char* pEmailId);
		int registerNewUser(const char* pEmailId, const char* pPassword,
				const char* pName, char gender, const char*pAddress, 
				const char* pCity, const char* pState, const char* pCountry, 
				const char* pZip, const char* pPhone, const char* pDob, 
				const char* pImId, const char* pImService, const char* pRegConfCode);
		int updateUserDetails(const char* pEmailId, const char* pImId, const char* pName,
				const char*pAddress, const char* pCity, const char* pState,
				const char* pCountry, const char* pZip, const char* pPhone,
				const char* pGender, const char* pDOb);
//		int fetchUserId(const char* emailId, string& userId);
		int confirmUserRegistration(const char* pEmailId);
		int updateLastAccessTime(const char* pEmailId);
		int updateLastAccessTime(const char* pImId, const char* pImService);
		char getUserRegistrationStatus(const char* pEmailId);
		int areUserCredentialsCorrect(const char* pEmailId, const char* pPassword);
		int isUserRegnConfCodeCorrect(const char* pEmailId, const char* pConfCode);
		int changePassword(const char* pEmailId, const char* pCurrPassword, const char* pNewPassword);
		int resetPassword(const char* pEmailId, const char* pNewPassword);
		int fetchPassword(const char* pEmailId, std::string& strPassword);
		int addInfo(const char* pEmailId, const char* pInfo, int media, const char* pExpiryDate, unsigned long ttl);
//		int addInfo(const unsigned long& userId, const char* pInfo, const int& media, const char* pExpiryDate, unsigned long& ttl);
		//int fetchInfoId(const unsigned long& userId, const char* pInfo, const int& media, const char* pExpiryData, const unsigned long& ttl, unsigned long& infoId);
		int fetchInfoId(const char* pEmailId, const char* pInfo, const int& media, const char* pExpiryData, const unsigned long& ttl, std::string& strInfoId);
		int updateInfo(const unsigned long& nInfoId, const char* pInfo, const char* pDateOfExpiry, unsigned long& ttl);
		int deleteInfo(const unsigned long& infoId, const char* pEmailId);
		int isRegisteredInfo(const unsigned long& infoId, const char* pEmailId);
		int isRegisteredRecipient(const unsigned long& recipientId, const char* pEmailId);
		int registerRecipient(const char* pRecipientName, const char* pRecipientEmailId, const char* pRecipientPhone, const char* pRecipientAddress, const char* pRelationWithOwner, const char* pOwnerEmailId);
		int fetchRecipientId(const char* pRecipientName, const char* pRecipientAddress, const char* pRecipientEmailId, const char* pRecipientPhone, const char* pRelationWithOwner, const char* pOwnerEmailId, std::string& strRecipientId); 
		int updateRecipient(const unsigned long& recipientId, const char* pRecipientName, const char* pRecipientEmailId, const char* pRecipientPhone, const char* pRecipientAddress, const char* pRecipientRelationship);
		int deleteRecipient(const unsigned long& recipientId, const char* pOwnerEmailId);
		int addInfoRecipient(const unsigned long& infoId, const unsigned long& recipientId);
		int deleteInfoRecipient(const unsigned long& recipientId, const unsigned long& infoId); 
		int fetchUserDetails(const char* pEmailId, std::string& strName, std::string& strAddress, std::string& strCity, std::string& strState, std::string& strCountry, std::string& strZip, std::string& strPhone, std::string& strDob, std::string& strGender, std::string& strImId, std::string& imService);
		int fetchRecipientList(const char* pEmailId, std::vector<RECIPIENT_DETAILS*>& recipientList);
		int fetchRecipientList(unsigned long infoId, std::vector<unsigned long>& recpientList); 
		int fetchInfoList(const char* pEmailId, std::vector<INFO_DETAILS*>& infoList);
		int fetchImId(const char* pEmailId, std::string& strImId);
		int setBuddyAddedStatus(const char* pEmailId, const char* pStatus);
	private:
		ImaDbInterface(ImaConf& conf);
		int connectToDb();
		char mDbHostname[256];
		int mDbPort;
		char mDbName[256];
		char mDbUsername[256];
		char mDbPassword[256];
		PGconn *mpConn;

		static ImaDbInterface* mpDbInterface;
		int deleteInfoIdReferences(const unsigned long& infoId);	//TODO: replace this function with a trigger in the DB 
		int deleteRecipientIdReferences(const unsigned long& recipientId);	//TODO: replace this function with a trigger in the DB 
};

#endif //__IMA_DB_INTERFACE_H__
